package com.deyou.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.deyou.R;
import com.deyou.model.CityPoiBean;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yangyuancheng on 2017/8/16.
 */

public class AutoRankAdapter extends RecyclerView.Adapter<AutoRankAdapter.ViewHolder> {

    private Context mContext;
    private int selectedId;
    private List<String> conditionList = new ArrayList<>();
    private OnItemClickListener itemClickListener;

    public void setConditionList(List<String> conditionList) {
        this.conditionList = conditionList;
        notifyDataSetChanged();
    }

    public void setSelectedId(int selectedId) {
        this.selectedId = selectedId;
        notifyDataSetChanged();
    }

    public AutoRankAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = LayoutInflater.from(mContext).inflate(R.layout.auto_rank_item, parent, false);
        return new ViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (selectedId == position){
            holder.tvConditionValue.setTextColor(mContext.getResources().getColor(R.color.color_FF832D));
            holder.icSelectedCondition.setVisibility(View.VISIBLE);
        } else {
            holder.tvConditionValue.setTextColor(mContext.getResources().getColor(R.color.color_272424));
            holder.icSelectedCondition.setVisibility(View.GONE);
        }
        holder.tvConditionValue.setText(conditionList.get(position));
        holder.mRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(position);
            }
        });
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return null == conditionList ? 0 : conditionList.size();
    }

    static class ViewHolder extends BaseViewHolder {
        @BindView(R.id.tv_rank_condition)
        TextView tvConditionValue;
        @BindView(R.id.ic_selected_condition)
        ImageView icSelectedCondition;
        @BindView(R.id.root_view)
        RelativeLayout mRootView;

        ViewHolder(View view) {
            super(view);
            AutoUtils.autoSize(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }

    public void setOnItemClick(OnItemClickListener listener){
        this.itemClickListener = listener;
    }
}
